package cn.doit.dat06

object _02_热身小练习 {
  def main(args: Array[String]): Unit = {
    val array: Array[Any] = Array(1, "宝明", 2, "xiangge", true, 'c',"浩哥")
    //需求：我要将里面的int类型的值都 *520 ==》Array(520,1040)
    //不行
/*    val res: Array[AnyVal] = for (elem <- array) yield {
      val bool: Boolean = elem.isInstanceOf[Int]
      if (bool) {
        val i: Int = elem.asInstanceOf[Int]
        i * 520
      }else{
        0
      }
    }*/
    val array1: Array[Any] = array.filter(_.isInstanceOf[Int])
    array1.map(elem=>{
      elem.asInstanceOf[Int]*520
    }).foreach(println)

    val parFun: PartialFunction[Any, Int] = new PartialFunction[Any, Int] {
      //类似于filter  这个方法返回的是一个bool类型的值，如果说返回的是true，就会进入到apply
      //如果返回的是false，就会将这个数值丢掉
      override def isDefinedAt(x: Any) = x.isInstanceOf[Int]
      //类似于map
      override def apply(v1: Any) = v1.asInstanceOf[Int] *520
    }

    val res1: Array[Any] = array.collect {
      case a: Int => a * 520
      case b: String =>
      case c: Char =>
      case _ =>
    }
    res1.foreach(println)


  }

}
